<template>
    <view class="rx-time">
        <div  class="rx-disabled" v-if="readOnly">{{ getCurrentValue(false) }}</div>
        <div v-else class="rx-time-select">
            <span @click="showPlugin" :style="[value?'color: #555555':'color:#ccc']" class="rx-time-name">{{ getCurrentValue(true) }}</span>
            <my-icon class="rx-date-icon" @click="onAllowClear" :type="value?'icon-lineicon-test28':'icon-lineVue3_date'"></my-icon>
        </div>
        <lingfeng-timepicker ref="time" :type="dateMode" :defaultData="defaultData"
                             @change="confirm"></lingfeng-timepicker>
    </view>

</template>

<script>
import MForm from '../js/MForm';

export default {

    name: "MTime",
    mixins: [MForm],
    data() {
        return {
            dateMode: "time",
            defaultData: {
                startTime: ""
            }
        }
    },
    created() {
        this.init();
    },
    methods: {
        getCurrentValue(val) {
            if (this.defaultData.startTime == "") {
                return val?this.$ti18('rxDecorator.select', "请选择"):'';
            } else {
                return this.defaultData.startTime
            }
        },
        showPlugin() {
            this.$refs.time.show();
        },
        init() {
            let conf = this.metadata.options;
            //获取时间格式
            let format = conf.format;
            if (format == 'HH:mm') {
                this.dateMode = "hour-minute";
            }
            this.setCurValue(this.value);
        },
        confirm(value) {
            if (value instanceof Object && value.hasOwnProperty("show")) {
                return;
            }
            this.setData(value);
        },
        onAllowClear(){
            this.setData('');
        },
        setCurValue(val){
            this.defaultData.startTime = val;
        }
    },
    watch: {
        value: function (val, oldVal) {
            this.setCurValue (val);
        }
    },
}
</script>

<style scoped="scoped">
.rx-time-select{
    border-radius: 5px;
    border: 0.03rem solid rgba(232, 232, 232, 1);
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 0.5rem 0.5rem;
}
.rx-time-name{
    flex: 1;
    color: #555555;
    margin-right: 0.5rem;
}
.rx-disabled{
    background: rgba(245, 246, 249, 1);
    display: flex;
    align-items: center;
    padding: 0.5rem;
    border-radius: 4px;
    border: none;
    min-height: 1rem;
}
.rx-date-icon{
    font-size: 0.9rem;
    color: rgba(0, 0, 0, 0.2);
}
</style>
